package com.cci.kangdao.service.impl;

import com.cci.kangdao.aop.UserThreadLocal;
import com.cci.kangdao.dao.ApprovalGoingOutTMapper;
import com.cci.kangdao.dao.WorkOrderTDao;
import com.cci.kangdao.dao.model.ApprovalGoingOutT;
import com.cci.kangdao.dao.model.UserT;
import com.cci.kangdao.dao.model.WorkOrderT;
import com.cci.kangdao.dto.BaseResponse;
import com.cci.kangdao.dto.request.ApprovalGoingOutTDto;
import com.cci.kangdao.service.ApprovalGoingOutTService;
import com.cci.kangdao.utilTool.CollectionUtils;
import com.cci.kangdao.utilTool.Date_Util;
import com.cci.kangdao.utilTool.LocalTimeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: joe
 * @company: CTY
 * @since: 2023/2/28 11:22
 * @version: V1.0
 **/
@Service
public class ApprovalGoingOutTServiceImpl implements ApprovalGoingOutTService {
    @Resource
    private ApprovalGoingOutTMapper approvalGoingOutTMapper;
    @Resource
    private WorkOrderTDao workOrderTDao;

    private int[] WORKORDER_MODIFIABLE = new int[]{2, 4, 6, 7, 8, 9, 10, 11, 12};

    @Override
    public BaseResponse save(ApprovalGoingOutTDto approvalGoingOutTDto) {
        UserT userT = UserThreadLocal.get();
        if (CollectionUtils.isEmpty(approvalGoingOutTDto.getList())) {
            return BaseResponse.error("请输入图片地址");
        }
        if (null == approvalGoingOutTDto.getOrderId()) {
            return BaseResponse.error("请输入orderId");
        }
        //验证工单是否 ；
        WorkOrderT workOrderT = workOrderTDao.getWorkOrderTByOrderId(approvalGoingOutTDto.getOrderId());
        if (null == workOrderT || !Arrays.stream(WORKORDER_MODIFIABLE).anyMatch(arrStatus -> arrStatus == workOrderT.getOrderStatus())) {
            return BaseResponse.error("工单不存在或工单状态不可编辑");
        }
        List<ApprovalGoingOutT> listByOrderId = approvalGoingOutTMapper.getListByOrderId(approvalGoingOutTDto.getOrderId(), 1);
        //将以前的旧数据置为删除的状态
        for (ApprovalGoingOutT approvalGoingOutT : listByOrderId) {
            approvalGoingOutT.setFlag(-1);
            approvalGoingOutT.setDeleter(userT.getId());
            approvalGoingOutT.setDeleteTime(LocalTimeUtil.getRealCurrentTime());
            approvalGoingOutTMapper.updateByPrimaryKeySelective(approvalGoingOutT);
        }
        //保存新的照片
        for (ApprovalGoingOutT approvalGoingOutT : approvalGoingOutTDto.getList()) {
            approvalGoingOutT.setOrderId(approvalGoingOutTDto.getOrderId());
            approvalGoingOutT.setFlag(0);
            approvalGoingOutT.setType(1);
            approvalGoingOutT.setCreator(userT.getId());
            approvalGoingOutT.setCreateTime(LocalTimeUtil.getRealCurrentTime());
            approvalGoingOutT.setVersion(1);
            approvalGoingOutTMapper.insert(approvalGoingOutT);
        }
        return BaseResponse.success();
    }

    @Override
    public BaseResponse getListByOrderId(Long orderId) {
        List<ApprovalGoingOutT> approvalGoingOutTList = approvalGoingOutTMapper.getListByOrderId(orderId, 1);
        if (CollectionUtils.isNotEmpty(approvalGoingOutTList)) {
            List<String> approvalGoingOutTStrlist = approvalGoingOutTList.stream().map(ApprovalGoingOutT::getApprovalPictureUrl).collect(Collectors.toList());
            return BaseResponse.success(approvalGoingOutTStrlist);
        }
        return BaseResponse.success();
    }

    @Override
    public List<String> getListByOrderIdReturnList(Long orderId) {
        List<ApprovalGoingOutT> approvalGoingOutTList = approvalGoingOutTMapper.getListByOrderId(orderId, 1);
        List<String> approvalGoingOutTStrlist = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(approvalGoingOutTList)) {
            approvalGoingOutTStrlist = approvalGoingOutTList.stream().map(ApprovalGoingOutT::getApprovalPictureUrl).collect(Collectors.toList());
        }
        return approvalGoingOutTStrlist;
    }
}
